package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.server.services.ITaskLogService;
import com.rykj.qxj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Log4j
@RestController
@RequestMapping("/v1/taskLog")
@Api(tags="任务异常统计")
public class TaskLogController {
    @Autowired
    private ITaskLogService taskLogService;

    @GetMapping("/getTaskExcInfos")
    @ApiOperation(value="分页获取任务异常记录列表",notes ="分页获取任务异常记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "taskName", value = "任务名称",paramType = "query"),
            @ApiImplicitParam(name = "tableName", value = "表名称",paramType = "query")
    })
    public Result getTaskExcInfos(@RequestParam(value = "pageNum") Integer pageNum,
                                  @RequestParam(value = "pageSize") Integer pageSize,
                                  @RequestParam(value = "taskName",required=false) String taskName,
                                  @RequestParam(value = "tableName",required=false) String tableName){
        try{
            PageInfo pageInfo =  taskLogService.getTaskExcInfos(pageNum,pageSize,taskName,tableName);
            return  Result.success(pageInfo);
        }catch (Exception e){
        log.error("获取任务异常记录列表失败", e);
        return new Result(false, "获取任务异常记录列表失败");
       }
    }

    @GetMapping("/getTaskExcFieldDetails")
    @ApiOperation(value="分页获取任务异常记录详情",notes ="分页获取任务异常记录详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "taskHistoryId", value = "任务运行历史记录id",required = true,paramType = "query")
    })
    public Result getTaskExcFieldDetails(@RequestParam(value = "pageNum") Integer pageNum,
                                  @RequestParam(value = "pageSize") Integer pageSize,
                                  @RequestParam(value = "taskHistoryId") Integer taskHistoryId){
        try{
            PageInfo pageInfo =  taskLogService.getTaskExcFieldDetails(pageNum,pageSize,taskHistoryId);
            return  Result.success(pageInfo);
        }catch (Exception e){
            log.error("获取任务异常记录详情失败", e);
            return new Result(false, "获取任务异常记录详情失败");
        }
    }

    @GetMapping("/getLogTaskDetails")
    @ApiOperation(value="获取任务详情",notes ="获取任务详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id",required = true,paramType = "query")
    })
    public Result getLogTaskDetails(@RequestParam(value = "taskId") Integer taskId){
        try{
            Map<String,Object> retMap =  taskLogService.getLogTaskDetails(taskId);
            return  Result.success(retMap);
        }catch (Exception e){
            log.error("获取任务详情失败", e);
            return new Result(false, "获取任务详情失败");
        }
    }



}
